package com.webdemo9.contrller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webdemo9.bean.UsersBean;
import com.webdemo9.dao.UsersDao;
import com.webdemo9.dao.impl.MD5Encryption;
import com.webdemo9.dao.impl.UsersDaoFactory;
/**
 * 更新用户信息
 * @author lovemanai
 *
 */
public class Update extends HttpServlet {

	private UsersDao usersDao = UsersDaoFactory.getUsersDao();
	
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String characterEncoding = this.getServletContext().getInitParameter("characterEncoding").toString();
		request.setCharacterEncoding(characterEncoding);
		String username,upwd,opwd,pwd,gender,birth,phone,addr;
		username = request.getParameter("username");
		upwd = request.getParameter("upwd");
		opwd = request.getParameter("opwd");
		pwd = request.getParameter("pwd");
		gender = request.getParameter("gender");
		birth = request.getParameter("birth");
		phone = request.getParameter("phone");
		addr = request.getParameter("addr");
		UsersBean ub = new UsersBean(); 
		ub.setUsername(username);
		ub.setPwd(pwd);
		ub.setGender(gender);
		ub.setBirth(birth);
		ub.setPhone(phone);
		ub.setAddr(addr);
		if(!MD5Encryption.MD5Encode(opwd).equals(upwd)) {
			response.sendRedirect("updateUser?username=" + username + "&errorcode=5");
			return;
		}
		if( usersDao.update(ub) != 0) {
			response.sendRedirect("listUsers");
		}
		
	}

}
